# 使用 Nginx Alpine 作为基础镜像
FROM nginx:alpine

# 设置维护者信息
LABEL maintainer="your-email@example.com"

# 安装基础工具
RUN apk add --no-cache \
    curl \
    wget \
    bash \
    vim \
    openssl \
    && rm -rf /var/cache/apk/*

# 创建自定义配置目录
RUN mkdir -p /etc/nginx/conf.d/custom \
    && mkdir -p /var/log/nginx \
    && mkdir -p /var/www/html

# 设置工作目录
WORKDIR /usr/share/nginx/html

# 创建默认首页
RUN echo '<h1>Welcome to Nginx Alpine</h1>' > /usr/share/nginx/html/index.html

# 设置权限
RUN chown -R nginx:nginx /var/www/html \
    && chown -R nginx:nginx /var/log/nginx

# 暴露端口
EXPOSE 80 443

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://localhost/ || exit 1

# 启动 Nginx
CMD ["nginx", "-g", "daemon off;"]

# ============================================
# Docker 镜像操作命令注释
# ============================================

# 1. 构建镜像
# docker build -t dockerfilenginx:alpine .

# 2. 运行容器测试
# docker run --rm -p 8080:80 dockerfilenginx:alpine

# 导出镜像为 tar 文件
# docker save -o dockerfilenginx-alpine.tar dockerfilenginx:alpine

# 传输 tar 文件到目标服务器
# scp dockerfilenginx-alpine.tar water@10.48.0.86:/home/water/docker/wenyu/

# 在目标服务器上导入镜像
# docker load -i dockerfilenginx-alpine.tar

# 查看导入的镜像
# docker images | grep dockerfilenginx

# 打 latest 标签
# docker tag dockerfilenginx:alpine 172.20.7.86:5001/webserver/nginx-alpine:latest

# 登录 Nexus 仓库
# docker login 172.20.7.86:5001
# admin
# admin123

# 推送镜像到仓库
# docker push 172.20.7.86:5001/webserver/nginx-alpine:latest

# 检查镜像仓库中的标签列表
# curl -u admin:admin123 -X GET "http://172.20.7.86:5001/v2/webserver/nginx-alpine/tags/list"

# 获取 latest 标签的镜像清单
# curl -u admin:admin123 -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET "http://172.20.7.86:5001/v2/webserver/nginx-alpine/manifests/latest"

# 检查仓库目录
# curl -u admin:admin123 -X GET "http://172.20.7.86:5001/v2/_catalog"

# 获取镜像的详细配置信息
# curl -u admin:admin123 -H "Accept: application/vnd.docker.distribution.manifest.v2+json" -X GET "http://172.20.7.86:5001/v2/webserver/nginx-alpine/manifests/latest" | jq .

# 如果您推送了多个标签，可以检查特定标签
# curl -u admin:admin123 -X GET "http://172.20.7.86:5001/v2/webserver/nginx-alpine/manifests/v1.0.0"

# 快速检查镜像是否存在（只返回状态码）
# curl -u admin:admin123 -I "http://172.20.7.86:5001/v2/webserver/nginx-alpine/manifests/latest"

# 从其他机器拉取镜像
# docker pull 172.20.7.86:5001/webserver/nginx-alpine:latest

# 验证拉取的镜像
# docker run --rm -p 8080:80 172.20.7.86:5001/webserver/nginx-alpine:latest